package ar.edu.unq.wannaplay.tools;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;

import ar.edu.unq.wannaplay.dtos.EmployeeDTO;

public class SortManager {
    private static SortManager instance;

    public static SortManager getInstance() {
        if (instance == null) { // NOPMD
            instance = new SortManager();
        }
        return instance;
    }

    public List<EmployeeDTO> sortByName(final List<EmployeeDTO> employees) {
        Comparator<EmployeeDTO> comparator = new Comparator<EmployeeDTO>() {
            @Override
            public int compare(final EmployeeDTO e1, final EmployeeDTO e2) {
                return e1.getName().equals(e2.getName()) ? 0 : e1.getName().compareTo(e2.getName()) > 0 ? 1 : -1;
            }
        };

        Queue<EmployeeDTO> queue = new PriorityQueue<EmployeeDTO>(employees.size(), comparator);
        queue.addAll(employees);

        List<EmployeeDTO> orderedEmployees = new ArrayList<EmployeeDTO>();

        while (!queue.isEmpty()) {
            orderedEmployees.add(queue.poll());
        }

        return orderedEmployees;
    }
}
